package com.yh.desk

import android.app.Presentation
import android.content.Context
import android.hardware.display.DisplayManager
import android.os.Bundle
import android.util.Log
import android.view.Display
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.yh.desk.ui.theme.MyYhDeskTheme

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            MyYhDeskTheme {
                Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
                    MainScreen(modifier = Modifier.padding(innerPadding))
                }
            }
        }
    }
}


@Composable
fun MainScreen(modifier: Modifier = Modifier) {
    Column(
        modifier = modifier
            .fillMaxSize()
            .padding(16.dp)
    ) {
        // 第一行
        Row(
            modifier = Modifier
                .weight(1f)
                .clip(RoundedCornerShape(16.dp)) // 设置圆角
        ) {
            RegionBox("区域 1")
        }
        Spacer(modifier = Modifier.height(16.dp))
        // 第二行
        Row(
            modifier = Modifier
                .weight(1f)
                .clip(RoundedCornerShape(16.dp))
        ) {
            RegionBox("区域 2")
        }
    }
}

@Composable
fun RegionBox(regionName: String) {
    val context = LocalContext.current
    Box(
        modifier = Modifier
            .fillMaxSize()
            .background(Color.LightGray)
            .clickable {
                showOnExternalDisplay(context, regionName)
            },
        contentAlignment = Alignment.Center
    ) {
        Text(text = regionName, fontSize = 24.sp)
    }
}

fun showOnExternalDisplay(context: Context, regionContent: String) {
    val displayManager = context.getSystemService(Context.DISPLAY_SERVICE) as DisplayManager
    val displays: Array<Display> =
        displayManager.getDisplays(DisplayManager.DISPLAY_CATEGORY_PRESENTATION)
    Log.d("TAG", "showOnExternalDisplay: $displays")
    if (displays.isNotEmpty()) {
        // 选择第一个外部显示器
        val display = displays[0]
        val presentation = ScreenPresentation(context, display) {
            Surface(color = Color.Black) {
                ContentToShow(regionContent)
            }
        }
        presentation.dismiss()
        presentation.show()
    } else {
        // 没有外部显示器连接，可以提示用户或其他处理
    }
}

@Composable
fun ContentToShow(text: String) {
    Box(
        modifier = Modifier.fillMaxSize(),
        contentAlignment = Alignment.Center
    ) {
        Text(text = text, color = Color.White, fontSize = 24.sp)
    }
}

// java.lang.IllegalStateException: ViewTreeLifecycleOwner not found from androidx.compose.ui.platform.ComposeView{29aa71d V.E...... ......I. 0,0-0,0}
class ScreenPresentation(
    context: Context,
    display: Display,
    private val content: @Composable () -> Unit
) : Presentation(context, display) {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val composeView = ComposeView(context).apply {
            setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
            setContent {
                MaterialTheme {
                    content()
                }
            }
        }
        setContentView(composeView)
    }
}